<template>
	<view class="update-box">

		<text class="progress-text"> {{progress}}%</text>

	</view>
</template>

<script>
	export default {
		name: "update",
		data() {
			return {
				progress: 0, //进度指示

			}
		},
		mounted() {


			// let sql1 =
			// 	"create table if not exists version ('id' INT(2),'ver' VARCHAR(255),'',time' INT(11))"; //新增版本管理的数据存储
			// let selectVer = "select * from version";

			// let insertVerData = "insert into version values" + data; //插入第一条记录
			/**
			 * 没有数据写入数据   检查 创建 
			 * 判断数据表version是否存在
			 * if不存在则创建这个表 并写入第一条更新记录
			 * ('id' INT(2),'ver' VARCHAR(255),'',time' INT(11))
			 * 初始化数据
			 */
			
			// let request_fn=(url,type,data)=>{
			// 	return new Promise((resolve, reject) => {
			// 		uni.request({
			// 			url: this.Domain + url,
			// 			method: type,
			// 			data: data,
			// 			header: {
			// 				'Content-Type': 'application/json'
			// 			},
			// 			success: res => {
			// 				resolve(res);
			// 			},
			// 			fail: (e) => {
			// 				reject(e);
			// 			},
			// 			complete: () => {}
			// 		});
				
			// 	});
				
			// }
			/**
			 * 请求版本更新数据接口
			 */
			// request_fn('','get',{}).then((res)=>{
			// },e=>{
				
			// })
			
			let check_ver = (...dat) => {
				let db_table = [
					"select * from " + dat[0] + " where (id=1)",
					"create table if not exists " + dat[0] + dat[1],
					"insert into version values " + dat[2]
				];
				let step = 0;
				let excult = () => {
						this.$DB.select(db_table[step]).then((dat) => {
							console.log(dat);
							switch (step) {
								case 0: //表示数据存在 数据表存在
									return false;
									break;
								case 1:
									step++;
									(step < db_table.length) ? excult(): "";
									break;
								case 2:
									step++;
									(step < db_table.length) ? excult(): "";
									break;
							}
						}, e => {
							console.log(db_table[step])
							console.log(e);
							step++;
							(step < db_table.length) ? excult(): "";


						});
					}
					(step < db_table.length) ? excult() : "";
			}
			let time = (new Date()).getTime();
			let data = "(1,0.01," + time + ")";
			check_ver('233', "('id' INT(2),'ver' VARCHAR(255),'time' INT(11))", data);
		},
		methods: {
			/**
			 * 热更新创建下载
			 */
			createDown() {
				let download = "https://wangjibu.top/web/static/wgt/ceshi2.wgt"; //热更新下载地址
				let downloadTask = uni.downloadFile({
					url: download,
					success: (res) => {
						if (res.statusCode == 200) {
							let install = () => {
								plus.runtime.install(res.tempFilePath, {
									force: true
								}, function() {
									console.log("install success...");
									plus.runtime.restart();
								}, function(e) {
									console.log('install fail...');
								});
							}
							uni.showModal({
								title: "更新提示",
								content: "是否确认更新？",
								success: (e) => {
									if (e.confirm) {
										install();
									}
								}
							})
						}
					}
				})
				downloadTask.onProgressUpdate((res) => {
					console.log(res)
					this.progress = res.progress;


				})


			}




		}


	}
</script>

<style scoped>
	.update-box {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		position: fixed;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(0, 0, 0, 0.2);
		color: white;
	}

	.progress-text {
		font-size: 45rpx;
		font-weight: 600;
	}
</style>
